"""
保险公司 CRUD 操作
"""
from typing import Optional, List
from tortoise.exceptions import DoesNotExist
from ..models.insurance_company import InsuranceCompany
from ..schemas.insurance_company import InsuranceCompanyDetailResponse
from ..models.user import User
from ..core.exceptions import UserNotFoundError
from datetime import date


class InsuranceCompanyCRUD:
    """
    保险公司 CRUD 操作
    """

    @staticmethod
    async def count_insurance_companies() -> int:
        """
        统计保险公司数量
        """
        return await InsuranceCompany.all().count()

    @staticmethod
    async def get_insurance_companies(skip: int = 0, limit: int = 100) -> List[dict]:
        """
        获取保险公司列表(需包含产品信息)
        """
        companies = await InsuranceCompany.all().prefetch_related("products").offset(skip).limit(limit)

        result = []
        for company in companies:
            # company_dict = InsuranceCompanyDetailResponse.model_validate(company).model_dump()
            # result.append(company_dict)
            company_dict = {
                "company_id": company.company_id,
                "company_name": company.company_name,
                "company_phone": company.phone,
                "company_email": company.email,
                "company_address": company.address,
                "products": [
                    {
                        "product_id": product.product_id,
                        "product_name": product.product_name,
                        "status": product.status
                    }
                    for product in company.products
                ]
            }
            result.append(company_dict)

        return result


























# 创建一个实例
insurance_companie_curd = InsuranceCompanyCRUD()